package com.medium.house.service.impl;

import java.util.List;
import java.util.Map;

import com.medium.house.response.HouseOverView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import com.medium.house.service.HouseInfoService;
import com.medium.house.repository.HouseInfoMapper;
import com.medium.house.entity.HouseInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;

@Service
public class HouseInfoServiceImpl implements HouseInfoService
{

    @Autowired
    private HouseInfoMapper houseInfoMapper;

    private static Map<String,String> piceRenge = new HashMap<String,String>();
    {
        piceRenge.put("21","0-500");
        piceRenge.put("22","500-1000");
        piceRenge.put("23","1000-1500");
        piceRenge.put("24","1500-2000");
        piceRenge.put("25","2000-3000");
        piceRenge.put("26","3000-4500");
        piceRenge.put("27","4500-1000000");
        piceRenge.put("11","0-300000");
        piceRenge.put("12","300000-400000");
        piceRenge.put("13","400000-500000");
        piceRenge.put("14","500000-600000");
        piceRenge.put("15","600000-700000");
        piceRenge.put("16","700000-800000");
        piceRenge.put("17","800000-5000000");
    }


    @Override
    public int add(HouseInfo dto)
    {
        return houseInfoMapper.insert(dto);
    }

    @Override
    public int delete(long id)
    {
        return houseInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(HouseInfo dto)
    {
        return houseInfoMapper.updateByPrimaryKeySelective(dto);
    }

    @Override
    public HouseInfo get(long id)
    {
        return houseInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<HouseInfo> getByPage(
        int start,
        int limit,
        int orderby,
        int order,
        int type,
        int status,
        String openid,
        String title
        )
    {
        if (start < 0) {start = 0;}
        if (limit <=0) {limit = 20;}
        if (orderby > HouseInfoMapper.ORDERBY.length - 1 || orderby < 0) {orderby = 0;}
        if (order != 0) {order = 1;}
        Map<String, Object> args = new HashMap<>();
        // Add more parameters here
        if (type > 0){
            args.put("type",type);
        }
        if (status > 0){
            args.put("status",status);
        }
        if (!StringUtils.isEmpty(openid)){
            args.put("openid",openid);
        }
        if (!StringUtils.isEmpty(title)){
            args.put("title",'%'+title+'%');
        }

        return houseInfoMapper.selectByPage(start, limit, HouseInfoMapper.ORDERBY[orderby], HouseInfoMapper.ORDER[order], args);
    }

    @Override
    public List<Long> getIdsByPage(
        int start,
        int limit,
        int orderby,
        int order)
    {
        if (start < 0) {start = 0;}
        if (limit <=0) {limit = 20;}
        if (orderby > HouseInfoMapper.ORDERBY.length - 1 || orderby < 0) {orderby = 0;}
        if (order != 0) {order = 1;}

        Map<String, Object> args = new HashMap<>();
        // Add more parameters here
        return houseInfoMapper.selectIdsByPage(start, limit, HouseInfoMapper.ORDERBY[orderby], HouseInfoMapper.ORDER[order], args);
    }

    @Override
    public long count(        int type,
                              int status,
                              String openid,
                              String title)
    {
        Map<String, Object> args = new HashMap<>();
        // Add more parameters here
        if (type > 0){
            args.put("type",type);
        }
        if (status > 0){
            args.put("status",status);
        }
        if (!StringUtils.isEmpty(openid)){
            args.put("openid",openid);
        }
        if (!StringUtils.isEmpty(title)){
            args.put("title",'%'+title+'%');
        }
        return houseInfoMapper.count(args);
    }

    @Override
    public List<HouseOverView> getByPageToOverView(
            int start,
            int limit,
            int orderby,
            int order,
            int type,
            int status,
            String openid,
            String title
    )
    {
        if (start < 0) {start = 0;}
        if (limit <=0) {limit = 20;}
        if (orderby > HouseInfoMapper.ORDERBY.length - 1 || orderby < 0) {orderby = 0;}
        if (order != 0) {order = 1;}
        Map<String, Object> args = new HashMap<>();
        // Add more parameters here
        if (type > 0){
            args.put("type",type);
        }
        if (status > 0){
            args.put("status",status);
        }
        if (!StringUtils.isEmpty(openid)){
            args.put("openid",openid);
        }
        if (!StringUtils.isEmpty(title)){
            args.put("title",'%'+title+'%');
        }
        return houseInfoMapper.selectByPageToOverView(start, limit, HouseInfoMapper.ORDERBY[orderby], HouseInfoMapper.ORDER[order], args);
    }

    @Override
    public List<Long> getIdsByPageToOverView(
            int start,
            int limit,
            int orderby,
            int order)
    {
        if (start < 0) {start = 0;}
        if (limit <=0) {limit = 20;}
        if (orderby > HouseInfoMapper.ORDERBY.length - 1 || orderby < 0) {orderby = 0;}
        if (order != 0) {order = 1;}

        Map<String, Object> args = new HashMap<>();
        // Add more parameters here
        return houseInfoMapper.selectIdsByPageToOverView(start, limit, HouseInfoMapper.ORDERBY[orderby], HouseInfoMapper.ORDER[order], args);
    }

    @Override
    public long countToOverView(        int type,
                              int status,
                              String openid,
                              String title)
    {
        Map<String, Object> args = new HashMap<>();
        // Add more parameters here
        if (type > 0){
            args.put("type",type);
        }
        if (status > 0){
            args.put("status",status);
        }
        if (!StringUtils.isEmpty(openid)){
            args.put("openid",openid);
        }
        if (!StringUtils.isEmpty(title)){
            args.put("title",'%'+title+'%');
        }
        return houseInfoMapper.countToOverView(args);
    }

    @Override
    public List<Map<String,Object>> getByPageToOverViewForJoin(
            int start,
            int limit,
            int orderby,
            int order,
            int type,
            int status,
            String openid,
            String title,
            String layout,
            String region,
            String price,
            String lease)
    {
        if (start < 0) {start = 0;}
        if (limit <=0) {limit = 20;}
        if (orderby > HouseInfoMapper.ORDERBY.length - 1 || orderby < 0) {orderby = 0;}
        if (order != 0) {order = 1;}
        Map<String, Object> args = new HashMap<>();
        // Add more parameters here
        if (type > 0){
            args.put("type",type);
        }
        if (status > 0){
            args.put("status",status);
        }
        if (!StringUtils.isEmpty(openid)){
            args.put("openid",openid);
        }
        if (!StringUtils.isEmpty(title)){
            args.put("title",'%'+title+'%');
        }
        if (!StringUtils.isEmpty(layout)){
            args.put("layout",layout);
        }
        if (!StringUtils.isEmpty(region)){
            args.put("region",region);
        }
        if (!StringUtils.isEmpty(price)){
            String priceStr = piceRenge.get(price);
            if (!StringUtils.isEmpty(priceStr)){
                String []  priceRange = priceStr.split("-");
                args.put("priceStart",priceRange[0]);
                args.put("priceEnd",priceRange[1]);
            }
        }
        if (!StringUtils.isEmpty(lease)){
            args.put("lease",lease);
        }
        return houseInfoMapper.selectIdsByPageToOverViewForJoin(start, limit, HouseInfoMapper.ORDERBY[orderby], HouseInfoMapper.ORDER[order], args);
    }

    public Map get(long id,int status){
        Map<String, Object> args = new HashMap<>();
        args.put("id",id);
        args.put("status",status);
        return houseInfoMapper.selectByIdAndStatus(args);
    }

}